﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.MT3DMS.DomainLayer;

namespace HA.ModFlow.Controls.MT3DMSWindows
{
    /// <summary>
    /// winMT3DStressPeriod.xaml 的交互逻辑
    /// </summary>
    public partial class winMT3DStressPeriod : Window
    {
        public winMT3DStressPeriod(MTStressPeriodCollecion stressPeriod)
        {
            InitializeComponent();
            this._mtStressPeriods = stressPeriod;
            this.StressPeriod = new List<MTStressPeriod>();
            foreach (MTStressPeriod item in stressPeriod.Periods)
            {
                this.StressPeriod.Add((MTStressPeriod)item.Clone());
            }


            if (this.StressPeriod.Count<=1)
            {
                this.btnDel.IsEnabled = false;
                
            }

            MenuItem itemCopy = new MenuItem();
            itemCopy.Header = "复制";
            itemCopy.Command = ApplicationCommands.Copy;

            //MenuItem itemPaste2 = new MenuItem();
            //itemPaste2.Header = "粘贴";
            //itemPaste2.InputGestureText = "Ctrl+V";
            //itemPaste2.Click += new RoutedEventHandler(itemPaste2_Click);

            ContextMenu cm = new ContextMenu();
            cm.Items.Add(itemCopy);
            //cm.Items.Add(itemPaste2);          
            this.mGrid.ContextMenu = cm;
        }

        MTStressPeriodCollecion _mtStressPeriods;


        List<MTStressPeriod> _StressPeriod;

        public List<MTStressPeriod> StressPeriod
        {
            get { return _StressPeriod; }
            set { _StressPeriod = value; }
        }

     


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            this.DataContext = this.StressPeriod;
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {           
            this._mtStressPeriods.ResetPeriods(this._StressPeriod);            
            this.DialogResult = true;
            this.Close();
        }

        private void btnCancal_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            int a = this.mGrid.SelectedIndex;
            if (a >= 0 && this.mGrid.Items.Count > 0)
            {
                this.StressPeriod.RemoveAt(a);
                this.DataContext = this.StressPeriod;
                this.mGrid.Items.Refresh();
                if (a >= this.mGrid.Items.Count)
                {
                    a = this.mGrid.Items.Count - 1;
                }
                this.mGrid.SelectedIndex = a;


                //加入最后一项不可删除
                if (this.StressPeriod.Count<=1)
                {
                    this.btnDel.IsEnabled = false;
                    
                }
            }
          
        }

        private void btnInsert_Click(object sender, RoutedEventArgs e)
        {
            int a = this.mGrid.SelectedIndex;

            if (a < 0)
            {
                a = 0;
            }
            this.StressPeriod.Insert(a, new MTStressPeriod());
            this.DataContext = this.StressPeriod;
            this.mGrid.Items.Refresh();
            this.mGrid.SelectedIndex = a;

            if (this.StressPeriod.Count>1)
            {
                this.btnDel.IsEnabled = true;
                
            }

           
        }

        private void mGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }

    }
}
